#include "ET++.ph"
#ifdef __GNUG__
#pragma implementation
#endif

#include "CollTblView.h"

#include "Class.h"
#include "ClassItem.h"
#include "ObjectTable.h"
#include "OrdColl.h"
#include "ObjArray.h"
#include "Collection.h"
#include "Alert_e.h"
#include "String.h"
#include "Dictionary.h"
#include "ET_stdio.h"

//---- PeCollTableView -----------------------------------------------------------

NewMetaImpl0(PeCollTableView, CollectionView);

PeCollTableView::PeCollTableView(int col)
									: CollectionView(0, 0, eCVDefault, 0, col)
{
}

PeObjectItem *PeCollTableView::AsObjectItem(Object *op)
{
	char buf[200], label[1000];
	op->InspectorId(buf, sizeof buf);

	sprintf(label, "0x%x %-14.14s: %s", (int) op,
										form("<%s>", op->ClassName()), buf);
	return new PeObjectItem(cIdNone, label, op);
}

void PeCollTableView::ShowCollection(Collection* col)
{
	if (col->Size() == 0)
		return;
	Iter next(col);
	OrdCollection *oc= new OrdCollection;
	Object *op;

	while (op= next())
		oc->Add(AsObjectItem(op));
	SetCollection(oc);
	Update();
	UpdateEvent();
}

void PeCollTableView::DoSelect(Rectangle r, int)
{
	Object *op, *sel= GetCollection()->At(r.origin.y);
	op= Guard(sel, PeObjectItem)->GetObject();
	if (ObjectTable::PtrIsValid(op))
		op->Inspect();
	else
		ShowAlert(eAlertNote, "instance 0x%x disappeard", op);
}

//---- PeDictionaryView ----------------------------------------------------------

NewMetaImpl0(PeDictionaryView, PeCollTableView);

PeDictionaryView::PeDictionaryView() : PeCollTableView(2)
{
}

void PeDictionaryView::ShowDictionary(Dictionary *dict)
{
	if (dict->Size() == 0)
		return;
	DictionaryIterator next(dict);
	Object *key;

	OrdCollection *oc= new OrdCollection(dict->Size() * 2);
	while ( key= next() ) {
		oc->Add(AsObjectItem(key));
		oc->Add(AsObjectItem(next.GetValue()));
	}

	SetCollection(oc);
	Update();
	UpdateEvent();
}

